<template>
    <div class="login-container">
      <div class="login-box">
        <h2>用户登录</h2>
        <form @submit.prevent="handleLogin">
          <div class="form-group">
            <label for="username">用户名</label>
            <input 
              type="text" 
              id="username" 
              v-model="loginForm.username" 
              placeholder="请输入用户名"
            >
          </div>
          <div class="form-group">
            <label for="password">密码</label>
            <input 
              type="password" 
              id="password" 
              v-model="loginForm.password" 
              placeholder="请输入密码"
            >
          </div>
          <button type="submit" class="login-btn">登录</button>
        </form>
        <div v-if="message" class="message" :class="{ 'error': !isSuccess }">
          {{ message }}
        </div>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref } from 'vue';
  import axios from 'axios';
  
  const loginForm = ref({
    username: '',
    password: ''
  });
  
  const message = ref('');
  const isSuccess = ref(false);
  
  const handleLogin = async () => {
    // 简单验证
    if (!loginForm.value.username || !loginForm.value.password) {
      message.value = '用户名和密码不能为空';
      isSuccess.value = false;
      return;
    }
  
    try {
      const response = await axios.post('http://localhost:8080/api/login', loginForm.value);
      
      if (response.data.code === 200) {
        message.value = '登录成功';
        isSuccess.value = true;
        // 这里可以处理登录成功后的逻辑，比如跳转页面
        console.log('用户数据:', response.data.data);
      } else {
        message.value = response.data.message || '登录失败';
        isSuccess.value = false;
      }
    } catch (error) {
      message.value = '请求失败，请检查网络或联系管理员';
      isSuccess.value = false;
      console.error('登录错误:', error);
    }
  };
  </script>
  
  <style lang="scss" scoped>
  .login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f5f5f5;
  }
  
  .login-box {
    width: 400px;
    padding: 40px;
    background: white;
    border-radius: 8px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
    
    h2 {
      text-align: center;
      margin-bottom: 30px;
      color: #333;
    }
  }
  
  .form-group {
    margin-bottom: 20px;
    
    label {
      display: block;
      margin-bottom: 8px;
      font-weight: 500;
      color: #555;
    }
    
    input {
      width: 100%;
      padding: 12px;
      border: 1px solid #ddd;
      border-radius: 4px;
      font-size: 16px;
      transition: border-color 0.3s;
      
      &:focus {
        border-color: #409eff;
        outline: none;
      }
    }
  }
  
  .login-btn {
    width: 100%;
    padding: 12px;
    background-color: #409eff;
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 16px;
    cursor: pointer;
    transition: background-color 0.3s;
    
    &:hover {
      background-color: #66b1ff;
    }
  }
  
  .message {
    margin-top: 20px;
    padding: 10px;
    border-radius: 4px;
    text-align: center;
    
    &.error {
      color: #f56c6c;
      background-color: #fef0f0;
    }
    
    &:not(.error) {
      color: #67c23a;
      background-color: #f0f9eb;
    }
  }
  </style>